package org.snia.cdmi.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

import org.snia.cdmi.LinkSitePermission;

/**
 * Represents an container.
 */
@XmlRootElement(name = "Container")
public class MetaContainer extends ContainerWithAcl {
    private static final long serialVersionUID = -8646831898339939580L;
    
    /** 对应的文件UUID */
    private String uuid = null;
   
    /** 基于Web的访问权限 */
    private LinkSitePermission permission = null;
    
    
    /** 允许通过网站进行浏览 */
    private List<String> sites = null;    
    
    /** 用户的元数据信息 */
    private Map<String, String> metadata = null;
    
    /**
     * Create a container without any name specified
     */
    public MetaContainer() {}

    
    public List<String> getSites() {
        return sites;
    }

    public void setSites(List<String> sites) {
        this.sites = sites;
    }
    
    public Map<String, String> getMetadata() {
        return this.metadata;
    }
    
    public void setMetadata(Map<String, String> metadata) {
    	if(this.metadata == null){
    		this.metadata = new HashMap<String, String>();
    	}
    	if(metadata != null){
    		this.metadata.putAll(metadata);
    	}
    }
	
	public void setAccessControlPolicy(AccessControlList acl){
		if(this.acl == null){
			this.acl = new HashMap<String, String>();
		}
//		for (Map.Entry<String, Permission> m : acl.entrySet()) {
//			if(m.getValue().equals(Permission.NULL)){
//				this.acl.remove(m.getKey());
//			}else{
//				this.acl.put(m.getKey(), m.getValue().toString());
//			}
//		 }
		
        for (Grant gap : acl.getGrants()) {
			if(gap.getPermission().equals(Permission.NULL)){
				this.acl.remove(gap.getGrantee().getIdentifier());
			}else{
				this.acl.put(gap.getGrantee().getIdentifier(), gap.getPermission().toString());
			}
        }
	}
	
	public String getAccessControlPolicy(String key){
		return this.acl.get(key);
	}
	
    public LinkSitePermission getPermission() {
        return permission;
    }

    public void setPermission(LinkSitePermission permission) {
        this.permission = permission;
    }

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getUuid() {
		return uuid;
	}

}
